package com.demo2do.boyle.backend.service.customer.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.cache.accessor.PersistentAccessor;
import com.demo2do.boyle.backend.service.customer.CustomerService;
import com.demo2do.boyle.backend.service.impl.DataServiceImpl;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.customer.Source;
import com.demo2do.boyle.common.entity.customer.social.SocialAccount;
import com.demo2do.boyle.common.entity.customer.wallet.Wallet;
import com.demo2do.boyle.common.entity.system.tag.Tag;
import com.demo2do.core.entity.Result;
import com.demo2do.core.persistence.GenericDaoSupport;

/**
 * 
 * @author Wilson
 */
@Service("customerService")
public class CustomerServiceImpl extends DataServiceImpl<Customer> implements CustomerService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
	private PersistentAccessor persistentAccessor;

    /**
     * 
     * @param source
     * @return
     */
    @SuppressWarnings("unchecked")
    private String generateCustomerCode(Source source) {

        Integer sequence = 1;
        
        List<String> result = genericDaoSupport.searchForList("SELECT code FROM Customer WHERE code LIKE '" + source.getAlphabet() + "%' ORDER BY code DESC", 0, 1);
        
        if(!result.isEmpty()) {
            
            // get code from database search
            String code = result.get(0);
            
            if(code != null)
                sequence = NumberUtils.createBigDecimal(code.substring(1)).intValue() + 1;
        }
        
        // left pad zero chars
        return source.getAlphabet() + StringUtils.leftPad(sequence.toString(), 5, "0");
        
    }
	
	/* (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.customer.CustomerService#create(com.demo2do.boyle.common.entity.customer.Customer, java.util.List, java.util.List)
	 */
	public void create(Customer customer, List<Integer> lifeTagCodes, List<Integer> interestTagCodes) {
		
		// 1. generate code of customer
		String code = this.generateCustomerCode(Source.SYSTEM);
		
		// 2. generate customer tag
		CustomerTag customerTag = new CustomerTag(customer);
		
		// 3. initialize customer tag
		Tag ageTag = (Tag) persistentAccessor.getAgeValues().get(customer.getProfile().getAge().toString());
		
		Tag areaTag = (Tag) persistentAccessor.getAreaValues().get(customer.getProfile().getCity().getCode());
		
		Tag sexTag = (Tag) persistentAccessor.getSexValues().get(String.valueOf(customer.getProfile().getSex().ordinal()));
		
		customerTag.initialize(ageTag, areaTag, sexTag, lifeTagCodes, interestTagCodes);
		
		// 4. create wallet
		Wallet wallet = new Wallet(customer);
		
		// 5. initialize customer with code, tag and wallet
		customer.initialize(Source.SYSTEM, code, customerTag, wallet);
		
		genericDaoSupport.save(customer);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.customer.CustomerService#loadSocialAccount(java.lang.Integer)
	 */
	public SocialAccount loadSocialAccount(Integer id) {
		return genericDaoSupport.load(SocialAccount.class, id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.customer.CustomerService#editSocialAccount(com.demo2do.boyle.common.entity.customer.social.SocialAccount)
	 */
	public Result editSocialAccount(SocialAccount socialAccount) {
		Result result = new Result();
		
		try {
			genericDaoSupport.update(socialAccount);
			result.success().message("更改成功");
		} catch (Exception e) {
			result.fail().message("更改失败");
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.customer.CustomerService#editTag(com.demo2do.boyle.common.entity.customer.CustomerTag)
	 */
	public void editTag(CustomerTag customerTag) {
		genericDaoSupport.update(customerTag);
	}
}